<?php

namespace App\Service\Pay\SysType;

use App\Enums\BaseErrorCode;
use App\Exceptions\Custom\ResponseHttpException;
use App\Library\Rms\RmsShopApi;
use App\Models\Common\OperateLogModel;
use App\Models\Fee\FeeAnnualChangesModel;
use App\Models\Fee\FeeAnnualOrderModel;
use App\Remote\Rms\RmsShopRemote;
use Illuminate\Support\Facades\Log;


class PayAnnualService extends BasePaySysTypeService
{
    public function valid(): void
    {
        // 订单状态不对
        if ($this->orderModel->payStatus == FeeAnnualOrderModel::PAY_STATUS_OK) {
            throw new ResponseHttpException(BaseErrorCode::$ERROR_OCCURRED, null, "年费订单已支付");
        }
    }

    public function priceLists(): array
    {
        $reduceManageFee = $reduceSystemFee = 0;

        // 减免金额
        $changes = FeeAnnualChangesModel::query()
                                        ->where('feeOrderNo', $this->orderModel->feeOrderNo)
                                        ->whereIn('field', ['manageFee', 'systemFee'])
                                        ->get()
                                        ->map(function ($item) use (&$reduceManageFee, &$reduceSystemFee) {

                                            $price = $item->beforeValue - $item->afterValue;
                                            if ($item->field == 'manageFee') {
                                                $name            = '管理费减免';
                                                $reduceManageFee += $price;
                                            } elseif ($item->field == 'systemFee') {
                                                $name            = '夸父运营数字化系统年服务费减免';
                                                $reduceSystemFee += $price;
                                            }

                                            $name = $item->remark ?: $name;

                                            return [
                                                'name'  => $name,
                                                'price' => $price * (-1),
                                                'image' => $item->image,
                                            ];
                                        });

        return collect([
            [
                'name'  => '管理费',
                'price' => $this->orderModel->manageFee + $reduceManageFee,
            ],
            [
                'name'  => '夸父运营数字化系统年服务费',
                'price' => $this->orderModel->systemFee + $reduceSystemFee,
            ],
        ])->merge($changes)->filter(function ($item) {
            if ($item['price'] == 0) {
                return false;
            }

            return true;
        })->values()->toArray();
    }

    public function paySuccessCheck(): bool
    {
        // 主订单已支付完成、直接返回成功 - 幂等
        if ($this->orderModel->payStatus == FeeAnnualOrderModel::PAY_STATUS_OK) {
            return true;
        }

        return false;
    }

    public function paySuccessSaveOrder(int $amount): void
    {
        $this->orderModel->onePaySuccess($amount);
    }

    // 设计费支付成功要干的事
    public function paySuccessAfter(): void
    {
        if ($this->orderModel->payStatus != FeeAnnualOrderModel::PAY_STATUS_OK) {
            return;
        }

        $cashRegisterStatus =  (new RmsShopApi())->getShopByIds([$this->orderModel->shopId])['data']['shopList'][0]['cashRegisterStatus'] ?? RmsShopRemote::SHOP_CASH_REGISTER_STATUS_OK;
        if ($cashRegisterStatus == RmsShopRemote::SHOP_CASH_REGISTER_STATUS_FEE_OFF) {
            //                CruiseShopService::updateCashRegisterStatusByShopId($this->orderModel->shopId, RmsShopRemote::SHOP_CASH_REGISTER_STATUS_OK);
            RmsShopRemote::updateShopFieldById($this->orderModel->shopId, ['cashRegisterStatus' => RmsShopRemote::SHOP_CASH_REGISTER_STATUS_OK]);
            Log::info('年费订单支付成功 将收银修改为正常状态');
        }

        // TODO
    }
}
